package com.woniuxy.main.service.impl;

import com.woniuxy.main.mapper.*;
import com.woniuxy.main.pojo.*;
import com.woniuxy.main.service.OrderService;
import com.woniuxy.main.utils.DateUtils;
import com.woniuxy.main.utils.GenerateOrderNumber;
import com.woniuxy.main.utils.OperationBigDecimal;
import com.woniuxy.main.utils.OperationEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Copyright (C), 2019-9012, sun sss info. Co., Ltd.
 *
 * @author Su
 * @version 1.0
 * @ClassName: OrderServiceImpl
 * @Description: 订单业务层实现类
 * @date 2019/8/1 15:30
 **/
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    RoomMapper roomMapper;
    @Autowired
    OrderListMapper orderListMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    CheckInMapper checkInMapper;

    @Override
    public List<Order> findAll() throws Exception {
        return orderMapper.findAll();
    }


    @Override
    public List<Order> findByOrdernum(String ordernum) throws Exception {
        return orderMapper.findByOrdernum(ordernum);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = false)
    public boolean addOrder(String startdate, String enddate, List<String> roomIds) throws Exception {
        //1.得到订单对象
        Order order = new Order();
        //1.1设置 订单号
        order.setOrdernum(GenerateOrderNumber.generateOrder());
        //1.2设置 支付方式为下线
        order.setPaynum("线下支付");
        //1.3 设置 入住天数
        int day = DateUtils.getDaysBetween(startdate, enddate);
        order.setDay(day);

        //1.3得到  总金额等于总房费+总押金  总押金

        //1.2.1 需支付金额 下线等于押金+房费
        BigDecimal prices = new BigDecimal(0);
        //1.3.2总押金
        BigDecimal deposits = new BigDecimal(0);
        for (String roomId : roomIds) {
            Room room = roomMapper.findRoomByID(Integer.valueOf(roomId));
            double price = room.getRoomtype().getPrice();
            //1.3.3 通过单价得到总每日房费
            prices = OperationBigDecimal.operationASMD(prices, room.getRoomtype().getPrice(),
                    OperationEnum.ADD, 2, BigDecimal.ROUND_DOWN);
            //1.3.3 通过押金得到总押金
            deposits = OperationBigDecimal.operationASMD(deposits, room.getRoomtype().getDeposit(),
                    OperationEnum.ADD, 2, BigDecimal.ROUND_DOWN);
        }
        //1.3.4 得到全部总房费
        prices =OperationBigDecimal.operationASMD(prices, day,
                OperationEnum.MULTIPLY, 2, BigDecimal.ROUND_DOWN);
        //1.3.5得到 总金额
        prices = OperationBigDecimal.operationASMD(prices, deposits,
                OperationEnum.ADD, 2, BigDecimal.ROUND_DOWN);
        //1.4设置 总金额
        order.setMoney(prices);
        //1.5设置 总押金
        order.setDeposit(deposits.intValue());
        //1.6设置 入住时间
        order.setStartdate(startdate);
        //1.7设置 拟退房时间
        order.setEnddate(enddate);
        //1.8 设置 当前登录人信息 待修改 Object o = SecurityUtils.getSubject().getPrincipal();
        order.setStaff_id(1001);
        //1.9设置订单状态 为入住成功
        order.setStatus(4);

        //2.生成新订单
        boolean flag = orderMapper.addOrder(order);
        //2.1 通过订单号 得到生成的订单 用于获取id号
        List<Order> orders = orderMapper.findByOrdernum(order.getOrdernum());
        Integer order_id = orders.get(0).getOrder_id();
        //3.为订单项表 添加数据
        OrderList orderList=null;
        for (String roomId : roomIds) {
            orderList.setOrder_id(order_id);
            orderList.setRoom_id(Integer.valueOf(roomId));
            boolean b = orderListMapper.addOrderList(orderList);
        }

         return flag;
    }


    @Override
    public boolean updateOrder(String order_id, Integer status) throws Exception {
        //待完善逻辑判断
        return orderMapper.updateOrder(order_id, status);
    }



    @Override
    public FinallyOrderDTO createOrder(String order_id) throws Exception {
        FinallyOrderDTO finallyOrderDTO = new FinallyOrderDTO();

        //订单
        Order order = orderMapper.findByIdAndStauts(order_id);

        if (order.getOrdernum()==null){
            System.out.println("订单号是空的");
            return null;
        }
        finallyOrderDTO.setOrder(order);

        //订单项
        List<OrderList> orderLists = orderListMapper.findByorderId(order_id);

        finallyOrderDTO.setOrderLists(orderLists);

        //用户信息
        User user = userMapper.findById(order.getUser_id());

        finallyOrderDTO.setUser(user);

        //房间
        Integer orderid=Integer.valueOf(order_id);
        ArrayList<Integer> oid = new ArrayList<>();
        oid.add(orderid);
        List<Room> room = roomMapper.findRoomByOrderId(oid);
        finallyOrderDTO.setRoom(room);

        //入住信息
        List<CheckIn> checkIns=new ArrayList<>();
        for (Room r : room) {
            int room_id = r.getRoom_id();
            List<CheckIn> check = checkInMapper.findByRoomid(room_id);
            for (CheckIn checkIn : check) {
                checkIns.add(checkIn);
            }
        }
        finallyOrderDTO.setCheckIn(checkIns);

        return finallyOrderDTO;
    }

    /*
    确认订单
     */
    @Override
    public boolean chagneStuts(String order_id) throws Exception{
        System.out.println(order_id);

        return orderMapper. updateOrder(order_id,8);
    }


}
